#include <cstdio>
#include <vector>
#define N 6010
using namespace std;

int n;
vector< int > son[N];
int val[N], f[N][2];
bool v[N];
void dfs(int x) {
	f[x][0] = 0;
	f[x][1] = val[x];
	for (register unsigned int i = 0; i < son[x].size(); ++i) {
		dfs(son[x][i]);
		f[x][0] += max(f[son[x][i]][0], f[son[x][i]][1]);
		f[x][1] += f[son[x][i]][0];
	}
}
int main() {
	freopen("rabbit.in", "r", stdin);
	freopen("rabbit.out", "w", stdout);
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &val[i]);
	}
	int a = 0, b = 0;
	while (scanf("%d%d", &a, &b) != EOF && a != 0 && b != 0) {
		son[b].push_back(a);
		v[a] = true;
	}
	int root = 0;
	for (int i= 1; i <= n; ++i) {
		if (!v[i]) {
			root = i;
		}
	}
	dfs(root);
	printf("%d", max(f[root][0], f[root][1]));
	fclose(stdin);
	fclose(stdout);
	return 0;
}